package com.yexiong.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.common.dao.BaseDao;
import com.common.entity.StuSubject;
import com.common.entity.Student;
import com.yexiong.dao.IStuSubjectDao;

public  class StuSubjectDaoImpl extends BaseDao implements IStuSubjectDao{

	@Override
	public Object getEntity(ResultSet rs) {
		StuSubject stuSubject=new StuSubject();
		try {
			
				stuSubject.setStuSubjectId(rs.getInt("id"));
				stuSubject.setSubNumber(rs.getInt("sub_number"));
				stuSubject.setStuId(rs.getInt("stu_id"));
				stuSubject.setSubDetailNumber(rs.getString("sub_detail"));
				stuSubject.setDuration(rs.getDouble("duration"));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stuSubject;
	}

	@Override
	public int update(StuSubject stuSubject) {
		int result=0;
		String sql="update stu_subject_details set duration=? where sub_number=?,sub_detail=?";
		Object[] objects= {stuSubject.getDuration(),stuSubject.getStuId(),stuSubject.getSubDetailNumber()};
		result=excuteUpdate(sql, objects);
		return result;
		
	}

	@Override
	public  List<StuSubject> select(int stuId) {
		List<StuSubject> stuSubjects=new ArrayList<>();
		String sql="select * from stu_subject_details where stu_id=? order by sub_number";
		stuSubjects=extuteQuery(sql, stuId);
		return stuSubjects;
		
	}

	@Override
	public double getDuration(StuSubject stuSubject) {
		List<StuSubject> stuSubjects=new ArrayList<>();
		String sql="select * from stu_subject_details where sub_number=?,sub_detail=?";
		stuSubjects=extuteQuery(sql, stuSubject.getStuId(),stuSubject.getSubDetailNumber());
		return stuSubjects.get(0).getDuration();
	}

	public int add(List<StuSubject> stuSubs) {
		int results=0;
		String sql = "INSERT INTO stu_subject_details(stu_id,sub_number,sub_detail,duration) VALUES(?,?,?,?)";

		for(int i= 0 ;i<stuSubs.size();i++) {
			List<Object>obs = new ArrayList<>();
			obs.add(stuSubs.get(i).getStuId());
			obs.add(stuSubs.get(i).getSubNumber());
			obs.add(stuSubs.get(i).getSubDetailNumber());
			
			obs.add(stuSubs.get(i).getDuration());
			int result = excuteUpdate(sql, obs.toArray());
			if(result>0) {
				results ++;
			}
		}
		return results;
	}

	@Override
	public double currentRestDuration(int stuId, int SubNmber) {
		double result=0.0;
		List<StuSubject> stuSubjects=new ArrayList<>();
		String sql="select * from stu_subject_details where stu_id=?,sub_number=?";
		stuSubjects=extuteQuery(sql, stuId,SubNmber);
		for (int i = 0; i <stuSubjects.size() ; i++) {
			result+=stuSubjects.get(i).getDuration();
			
		}
		return result;
	}

	@Override
	public int delete(List<Integer> StuIds) {
		int results=0;
		String sql = "DELETE FROM stu_subject_details WHERE stu_id =?";
		for(int i= 0;i<StuIds.size();i++) {
			int result = excuteUpdate(sql, StuIds.get(i));
			if(result >0) {
				results++;
			}
		}
		return results;
	}

	

	
	

}
